__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. urllib3 - Thread-safe connection pooling and re-using.
  3. """
  4. from __future__ import absolute_import
  5. import warnings
  6. from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  7. from . import exceptions
  8. from .filepost import encode_multipart_formdata
  9. from .poolmanager import PoolManager, ProxyManager, proxy_from_url
  10. from .response import HTTPResponse
  11. from .util.request import make_headers
  12. from .util.url import get_host
  13. from .util.timeout import Timeout
  14. from .util.retry import Retry
  15. # Set default logging handler to avoid "No handler found" warnings.
  16. import logging
  17. from logging import NullHandler
  18. __author__ = "Andrey Petrov (andrey.petrov@shazow.net)"
  19. __license__ = "MIT"
  20. __version__ = "1.25.9"
  21. __all__ = (
  22. "HTTPConnectionPool",
  23. "HTTPSConnectionPool",
  24. "PoolManager",
  25. "ProxyManager",
  26. "HTTPResponse",
  27. "Retry",
  28. "Timeout",
  29. "add_stderr_logger",
  30. "connection_from_url",
  31. "disable_warnings",
  32. "encode_multipart_formdata",
  33. "get_host",
  34. "make_headers",
  35. "proxy_from_url",
  36. )
  37. logging.getLogger(__name__).addHandler(NullHandler())
  38. def add_stderr_logger(level=logging.DEBUG):
  39. """
  40. Helper for quickly adding a StreamHandler to the logger. Useful for
  41. debugging.
  42. Returns the handler after adding it.
  43. """
  44. # This method needs to be in this __init__.py to get the __name__ correct
  45. # even if urllib3 is vendored within another package.
  46. logger = logging.getLogger(__name__)
  47. handler = logging.StreamHandler()
  48. handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
  49. logger.addHandler(handler)
  50. logger.setLevel(level)
  51. logger.debug("Added a stderr logging handler to logger: %s", __name__)
  52. return handler
  53. # ... Clean up.
  54. del NullHandler
  55. # All warning filters *must* be appended unless you're really certain that they
  56. # shouldn't be: otherwise, it's very hard for users to use most Python
  57. # mechanisms to silence them.
  58. # SecurityWarning's always go off by default.
  59. warnings.simplefilter("always", exceptions.SecurityWarning, append=True)
  60. # SubjectAltNameWarning's should go off once per host
  61. warnings.simplefilter("default", exceptions.SubjectAltNameWarning, append=True)
  62. # InsecurePlatformWarning's don't vary between requests, so we keep it default.
  63. warnings.simplefilter("default", exceptions.InsecurePlatformWarning, append=True)
  64. # SNIMissingWarnings should go off only once.
  65. warnings.simplefilter("default", exceptions.SNIMissingWarning, append=True)
  66. def disable_warnings(category=exceptions.HTTPWarning):
  67. """
  68. Helper for quickly disabling all urllib3 warnings.
  69. """
  70. warnings.simplefilter("ignore", category)